<template>
    <div class="list"><div>
        <div class="area">
            <div class="title border-topbottom">当前城市</div>
            <div class="button-list">
                <div class="button">{{this.$store.state.city}}</div>
            </div>
        </div>
        <div class="area">
            <div class="title border-topbottom">热门城市</div>
            <div class="button-list">
                <div class="button" v-for="city of hotCities"
                     :key="city.id"
                     @click="handleCityClick(city.name)"
                >{{city.name}}
                </div>
            </div>
        </div>
        <div class="area" v-for="(items, key) of cities" :key="key" :id="key">
            <div class="title border-topbottom">{{key}}</div>
            <div class="item-list">
                <div class="item border-bottom" v-for="(item, index) of items" :key="index"
                     @click="handleCityClick(item.name)"
                >{{item.name}}</div>
            </div>
        </div>
    </div></div>
</template>

<script>
    import BScroll from 'better-scroll'
    export default {
        name: "list",
        props: {
            hotCities: Array,
            cities: Object,
            letter: String
        },
        mounted() {
            this.bscroll=new BScroll('.list', {click: true})
        },
        watch: {
            letter(){
                if(this.letter){
                    this.bscroll.scrollToElement('#'+this.letter);
                }
            }
        },
        methods:{
            handleCityClick(city){
                // 标准用发：dispatch(事件名称, 事件参数)
                this.$store.dispatch('changeCity', city)

                // 短路用法：commit(事件名称, 事件参数)
                // this.$store.commit('changeCity', city)

                // 以下不推荐，违反了VUE的单向数据流规则
                // this.$store.state.city=city

                // 编程式导航，不同于标签式 <router-link to="/">
                this.$router.push('/')
            }
        }
    }
</script>

<style scoped lang="stylus">
    @import "~@styles/variables.styl"
    .border-topbottom
        &:before
            border-color:#ccc
        &:after
            border-color:#ccc
    .border-bottom
        &:before
            border-color:#ccc
    .list
        line-height: $lineHeight
        position: absolute
        top: $lineHeight*1.9
        right: 0
        left: 0
        bottom: 0
        overflow: hidden
        .area
            .title
                padding: 0 .2rem
                background-color: $bgColor_title
            .button-list
                padding: .1rem .6rem .1rem .1rem
                overflow: hidden
                .button
                    box-sizing: border-box
                    margin: .1rem
                    width: 29%
                    float: left
                    text-align: center
                    border: .02rem solid #ccc
                    border-radius: .1rem
            .item-list
                .item
                    padding: 0 .2rem

</style>